home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1993 / MacHack 1993.toast / MacHack™ 1987-1992 / MacHack™ '87 / Source ƒ / XLISP ƒ / XLISP 1.7 C SRCS / macstuff.c < prev    next >
Encoding:
C/C++ Source or Header  |  1986-07-07  |  9.9 KB  |  532 lines  |  [TEXT/EDIT]

  1. /* macstuff.c - macintosh interface routines for xlisp */
  2.  
  3. #include <qd.h>  
  4. #include <mem.h>
  5. #include <file.h>
  6. #include <font.h>
  7. #include <win.h>
  8. #include <menu.h>
  9. #include <dialog.h>
  10. #include <event.h>
  11. #include <pack.h>
  12. #include <stdio.h>
  13.  
  14. #define TRUE    1
  15. #define FALSE    0
  16.  
  17. #define appleid        1
  18. #define applemenu     0
  19. #define fileid        256
  20. #define filemenu    1
  21. #define editid        257
  22. #define editmenu    2
  23. #define controlid    258
  24. #define controlmenu     3
  25. #define lastmenu    4
  26.  
  27. extern char *s_unbound;
  28.  
  29. #include <qdvars.h>    /* quickdraw globals */
  30. MenuHandle mymenus[lastmenu];
  31. SFReply loadfile;
  32. OsType filetypes[] = { {'T','E','X','T'} };
  33. Rect dragrect;
  34. int x,y;
  35.  
  36. /* command window */
  37. WindowRecord cwrecord;
  38. WindowPtr cwindow;
  39.  
  40. /* graphics window */
  41. WindowRecord gwrecord;
  42. WindowPtr gwindow;
  43.  
  44. /* window mode */
  45. int splitmode;
  46.  
  47. #define TIMEON    500
  48. #define TIMEOFF    100
  49. int cursortime,cursorstate;
  50.  
  51. /* graphics window */
  52. #define GHORIZONTAL    4
  53. #define GVERTICAL    22
  54. #define GWIDTH        504
  55. #define GHEIGHT        232
  56.  
  57. /* split screen */
  58. #define SSCRW        80
  59. #define SSCRH        5
  60. #define SHORIZONTAL    4
  61. #define SVERTICAL    275
  62. #define SWIDTH        504
  63. #define SHEIGHT        64
  64.  
  65. /* normal screen */
  66. #define NSCRW        80
  67. #define NSCRH        24
  68. #define NHORIZONTAL    4
  69. #define NVERTICAL    41
  70. #define NWIDTH        504
  71. #define NHEIGHT        298
  72.  
  73. /* screen buffer */
  74. #define SCRH    24
  75. #define SCRW    80
  76. char screen[SCRH*SCRW],*topline,*curline;
  77. int scrh,scrw;
  78.  
  79. #define LINEMAX 200
  80. char linebuf[LINEMAX+1],*lineptr;
  81. int linepos[LINEMAX],linelen;
  82.  
  83. #define CHARMAX 100
  84. char charbuf[CHARMAX],*inptr,*outptr;
  85. int charcnt;
  86.  
  87. osinit(name)
  88.   char *name;
  89. {
  90.     Rect screenrect;
  91.     long cursp;
  92.     char *p;
  93.     
  94.     /* initialize the toolbox */
  95.     InitGraf(&thePort);
  96.     InitFonts();
  97.     InitWindows();
  98.     InitMenus();
  99.     TEInit();
  100.     InitDialogs(0L);
  101.     InitCursor();
  102.  
  103.     /* reserve 32K more for the stack */
  104.     asm { move.l A7,cursp(A6) }
  105.     SetApplLimit(cursp - 32768L);
  106.     
  107.     /* setup the menu bar */
  108.     setupmenus();
  109.  
  110.     /* setup the bounds rectangle for dragging windows */
  111.     SetRect(&dragrect,4,24,508,338);
  112.  
  113.     /* Create the graphics and control windows */
  114.     gwindow = GetNewWindow(129,&gwrecord,-1L);
  115.     cwindow = GetNewWindow(128,&cwrecord,-1L);
  116.  
  117.     /* establish the command window as the current port */
  118.     SetPort(cwindow);
  119.  
  120.     /* setup the font, size and writing mode for the command window */
  121.     TextFont(Monaco); TextSize(9); TextMode(srcCopy);
  122.     
  123.     /* setup command mode */
  124.     splitmode = FALSE;
  125.     scrh = SCRH;
  126.     scrw = SCRW;
  127.     scrclear();
  128.  
  129.     /* disable the Cursor */
  130.     cursorstate = -1;
  131.  
  132.     /* setup the input ring buffer */
  133.     inptr = outptr = charbuf;
  134.     charcnt = 0;
  135.     
  136.     /* setup the Line editor */
  137.     linelen = 0;
  138.  
  139.     /* lock the font in memory (need to write something first) */
  140.     for (p = name; *p; )
  141.     scrputc(*p++);
  142.     scrputc('\r'); scrputc('\n');
  143.     SetFontLock(TRUE);
  144. }
  145.  
  146. osfinish()
  147. {
  148. }
  149.  
  150. int osrand(n)
  151.   int n;
  152. {
  153.     return (rand() % n);
  154. }
  155.  
  156. int osgetc(fp)
  157.   FILE *fp;
  158. {
  159.     if (fp == stdin) return (linegetc());
  160.     else return (getc(fp));
  161. }
  162.  
  163. int osputc(ch,fp)
  164.   int ch; FILE *fp;
  165. {
  166.     oscheck();
  167.     if (fp == stdout) return (lineputc(ch));
  168.     else return (putc(ch,fp));
  169. }
  170.  
  171. setupmenus()
  172. {
  173.     int i;
  174.  
  175.     mymenus[applemenu]   = GetMenu(appleid); AddResMenu(mymenus[applemenu],"DRVR");
  176.     mymenus[filemenu]    = GetMenu(fileid);
  177.     mymenus[editmenu]    = GetMenu(editid);
  178.     mymenus[controlmenu] = GetMenu(controlid);
  179.     for (i = 0; i < lastmenu; i++)
  180.     InsertMenu(mymenus[i],0);
  181.     DrawMenuBar();
  182. }
  183.  
  184. int linegetc()
  185. {
  186.     int ch;
  187.  
  188.     if (linelen--) return (*lineptr++);
  189.     linelen = 0;
  190.     while ((ch = scrgetc()) != '\r')
  191.     switch (ch) {
  192.     case EOF:
  193.         return (linegetc());
  194.     case '\010':
  195.         if (linelen > 0) {
  196.         linelen--;
  197.         while (x > linepos[linelen]) {
  198.             scrputc('\010'); scrputc(' '); scrputc('\010');
  199.         }
  200.         }
  201.         break;
  202.     default:
  203.         if (linelen < LINEMAX) { linebuf[linelen] = ch; linepos[linelen] = x; linelen++; }
  204.         scrputc(ch);
  205.         break;
  206.     }
  207.     scrputc('\r'); scrputc('\n');
  208.     linebuf[linelen] = '\n';
  209.     lineptr = linebuf;
  210.     return (*lineptr++);
  211. }
  212.  
  213. int lineputc(ch)
  214.   int ch;
  215. {
  216.     if (ch == '\n') scrputc('\r');
  217.     scrputc(ch);
  218.     return (1);
  219. }
  220.  
  221. scrclear()
  222. {
  223.     curline = screen;
  224.     for (y = 0; y < SCRH; y++)
  225.     for (x = 0; x < SCRW; x++)
  226.         *curline++ = ' ';
  227.     topline = curline = screen;
  228.     x = y = 0;
  229. }
  230.  
  231. int scrgetc()
  232. {
  233.     CursorOn();
  234.     while (charcnt == 0)
  235.     oscheck();
  236.     CursorOff();
  237.     return (scrnextc());
  238. }
  239.  
  240. int scrnextc()
  241. {
  242.     int ch;
  243.     if (charcnt > 0) {
  244.     ch = *outptr++; charcnt--;
  245.     if (outptr >= &charbuf[CHARMAX])
  246.         outptr = charbuf;
  247.     }
  248.     else {
  249.     charcnt = 0;
  250.     ch = EOF;
  251.     }
  252.     return (ch);
  253. }
  254.  
  255. oscheck()
  256. {
  257.     WindowPtr whichwindow;
  258.     EventRecord myevent;
  259.     long sel;
  260.     
  261.     SystemTask();
  262.     CursorUpdate();
  263.  
  264.     while (GetNextEvent(everyEvent,&myevent))
  265.     switch (myevent.what) {
  266.         case mouseDown:
  267.         switch (FindWindow(&myevent.where,&whichwindow)) {
  268.             case inMenuBar:
  269.             if (sel = MenuSelect(&myevent.where))
  270.                 docommand(sel);
  271.             break;
  272.             case inSysWindow:
  273.             SystemClick(&myevent,whichwindow);
  274.             break;
  275.             case inDrag:
  276.             DragWindow(whichwindow,&myevent.where,&dragrect);
  277.             break;
  278.             case inGoAway:
  279.             if (TrackGoAway(whichwindow,&myevent.where))
  280.                 exit();
  281.             break;
  282.             case inGrow:
  283.             case inContent:
  284.             if (whichwindow != FrontWindow() && whichwindow != gwindow)
  285.                 SelectWindow(whichwindow);
  286.             break;
  287.         }
  288.         break;
  289.         case keyDown:
  290.         case autoKey:
  291.         if (cwindow == FrontWindow()) {
  292.             if (myevent.modifiers & 0x100) {
  293.             if (sel = MenuKey((char)myevent.message))
  294.                 docommand(sel);
  295.             }
  296.             else {
  297.             if (charcnt < CHARMAX) {
  298.                     *inptr++ = myevent.message & 0xFF; charcnt++;
  299.                 if (inptr >= &charbuf[CHARMAX])
  300.                 inptr = charbuf;
  301.             }
  302.             }
  303.         }
  304.         break;
  305.         case activateEvt:
  306.         break;
  307.         case updateEvt:
  308.         whichwindow = (WindowPtr) myevent.message;
  309.         BeginUpdate(whichwindow);
  310.         if (whichwindow == cwindow) 
  311.             doupdate();
  312.         EndUpdate(whichwindow);
  313.         break;
  314.         }
  315. }
  316.  
  317. CursorUpdate()
  318. {
  319.     if (cursorstate != -1)
  320.     if (cursortime-- < 0) {
  321.         scrposition(x,y);
  322.         if (cursorstate) {
  323.         DrawChar(' ');
  324.         cursortime = TIMEOFF;
  325.         cursorstate = 0;
  326.         }
  327.         else {
  328.         DrawChar('_');
  329.         cursortime = TIMEON;
  330.         cursorstate = 1;
  331.         }
  332.     }
  333. }
  334.  
  335. CursorOn()
  336. {
  337.     cursorstate = cursortime = 0;
  338. }
  339.  
  340. CursorOff()
  341. {
  342.     if (cursorstate == 1) {
  343.     scrposition(x,y);
  344.     DrawChar(' ');
  345.     }
  346.     cursorstate = -1;
  347. }
  348.  
  349. scrputc(ch)
  350.   int ch;
  351. {
  352.     if (ch == '\r') x = 0;
  353.     else if (ch == '\n') { nextline(&curline); if (y < scrh-1) y++; else scrollup(); }
  354.     else if (ch == '\t') { scrputc(' '); while (x & 7) scrputc(' '); }
  355.     else if (ch == '\010') { if (x) x--; }
  356.     else if (ch >= 0x20 && ch < 0x7F) {
  357.     scrposition(x,y);
  358.     DrawChar(ch);
  359.     curline[x] = ch;
  360.     if (x < scrw-1) x++;
  361.     else { x = 0; nextline(&curline); if (y < scrh-1) y++; else scrollup(); }
  362.     }
  363. }
  364.  
  365. scrflush()
  366. {
  367.     lineptr = linebuf; linebuf[0] = '\n'; linelen = 1;
  368.     inptr = outptr = charbuf;
  369.     charcnt = -1;
  370. }
  371.  
  372. scrposition(x,y)
  373.   int x,y;
  374. {
  375.     MoveTo((x * 6) + 4,(y * 12) + 12);
  376. }
  377.  
  378. pascal filefilter(pblock)
  379.   paramblkptr pblock;
  380. {
  381.     char *p; int len;
  382.     p = pblock->ioNamePtr; len = *p++ &0xFF;
  383.     return (len >= 4 && strncmp(p+len-4,".lsp",4) == 0 ? 0 : 0x100);
  384. }
  385.  
  386. pascal aboutfilter(theDialog,theEvent,itemHit)
  387.   DialogPtr theDialog; EventRecord *theEvent; int *itemHit;
  388. {
  389.     return (theEvent->what == mouseDown ? 0x100 : 0);
  390. }
  391.  
  392. docommand(themenu,theitem)
  393.   int themenu,theitem;
  394. {
  395.     DialogRecord mydialog;
  396.     char name[256];
  397.     GrafPtr gp;
  398.     Point p;
  399.     int n;
  400.     
  401.     CursorOff();
  402.     HiliteMenu(themenu);
  403.     switch (themenu) {
  404.     case appleid:
  405.     switch (theitem) {
  406.     case 1:
  407.         GetNewDialog(129,&mydialog,-1L);
  408.         ModalDialog(aboutfilter,&n);
  409.         CloseDialog(&mydialog);
  410.         break;
  411.     default:
  412.         GetItem(mymenus[0],theitem,name);
  413.         GetPort(&gp);
  414.         OpenDeskAcc(name);
  415.         SetPort(gp);
  416.         break;
  417.     }
  418.     break;
  419.     case fileid:
  420.     switch (theitem) {
  421.     case 1:    /* load */
  422.     case 2:    /* load noisily */
  423.         p.a.h = 100; p.a.v = 100;
  424.         SFGetFile(&p,"",filefilter,-1,filetypes,0L,&loadfile);
  425.         if (loadfile.good) {
  426.         HiliteMenu(0);
  427.         SetVol(0L,loadfile.vRefNum);
  428.         if (xlload(loadfile.fName,1,(theitem == 1 ? 0 : 1)))
  429.             scrflush();
  430.         else
  431.             xlabort("load error");
  432.         }
  433.         break;
  434.     case 4:    /* quit */
  435.         exit();
  436.     }
  437.     break;
  438.     case editid:
  439.     switch (theitem) {
  440.     case 1:    /* undo */
  441.     case 3:    /* cut */
  442.     case 4:    /* copy */
  443.     case 5:    /* paste */
  444.     case 6:    /* clear */
  445.         SystemEdit(theitem-1);
  446.         break;
  447.     }
  448.     break;
  449.     case controlid:
  450.     scrflush();
  451.     HiliteMenu(0);
  452.     switch (theitem) {
  453.     case 1:    /* break */
  454.         xlbreak("user break",s_unbound);
  455.         break;
  456.     case 2:    /* continue */
  457.         xlcontinue();
  458.         break;
  459.     case 3:    /* clean-up error */
  460.         xlcleanup();
  461.         break;
  462.     case 4:    /* Cancel input */
  463.         xlabort("input canceled");
  464.         break;
  465.     case 5:    /* Top Level */
  466.         xltoplevel();
  467.         break;
  468.     case 7:    /* split screen */
  469.         scrsplit(splitmode ? FALSE : TRUE);
  470.         break;
  471.     }
  472.     break;
  473.     }
  474.     HiliteMenu(0);
  475.     CursorOn();
  476. }
  477.  
  478. scrsplit(split)
  479.   int split;
  480. {
  481.     ShowHide(cwindow,0);
  482.     if (split) {
  483.     CheckItem(mymenus[controlmenu],7,1);
  484.     scrh = SSCRH; scrw = SSCRW;
  485.     scrclear();
  486.     MoveWindow(cwindow,SHORIZONTAL,SVERTICAL,1);
  487.     SizeWindow(cwindow,SWIDTH,SHEIGHT,1);
  488.     EraseRect(&cwindow->portRect);
  489.     ShowHide(gwindow,1);
  490.     }
  491.     else {
  492.     CheckItem(mymenus[controlmenu],7,0);
  493.     scrh = NSCRH; scrw = NSCRW;
  494.     scrclear();
  495.     MoveWindow(cwindow,NHORIZONTAL,NVERTICAL,1);
  496.     SizeWindow(cwindow,NWIDTH,NHEIGHT,1);
  497.     EraseRect(&cwindow->portRect);
  498.     ShowHide(gwindow,0);
  499.     }
  500.     ShowHide(cwindow,1);
  501.     splitmode = split;
  502. }
  503.  
  504. doupdate()
  505. {
  506.     char *Line; int y;
  507.     Line = topline;
  508.     for (y = 0; y < scrh; y++) {
  509.     scrposition(0,y);
  510.     DrawText(Line,0,scrw);
  511.     nextline(&Line);
  512.     }
  513. }
  514.  
  515. nextline(pline)
  516.   char **pline;
  517. {
  518.     if ((*pline += SCRW) >= &screen[SCRH*SCRW]) *pline = screen;
  519. }
  520.  
  521. scrollup()
  522. {
  523.     RgnHandle updateRgn;
  524.     int x;
  525.     updateRgn = NewRgn();
  526.     ScrollRect(&cwindow->portRect,0,-12,updateRgn);
  527.     DisposeRgn(updateRgn);
  528.     for (x = 0; x < SCRW; x++)
  529.     topline[x] = ' ';
  530.     nextline(&topline);
  531. }
  532.